import  wx
import  wx.grid as gridlib
from wx.lib.pubsub import Publisher as pub

from .panels import *
from genutils.get_logger import get_logger

__all__ = ['View','ExperimentsGrid','ExportDialog']

class View():
    "This class presents the API for the GUI."
    def __init__(self):
        # data viewer
        self.dv_frame = wx.Frame(None, title="Data Viewer", size=(900,650),
                            style=wx.MINIMIZE_BOX|wx.CAPTION)
        self.data_viewer = Data_viewer_panel(self.dv_frame, wx.ID_ANY)

        # data picker
        self.dp_frame = BigBrotherFrame(None, wx.ID_ANY, 
                                        title="Data Picker",
                                        size=(1440/3,(750*2)/3))
        self.data_picker = Data_picker_notebook(self.dp_frame, 
                                                logger_name='View_data')


    def show_frames(self, bool):
        self.dv_frame.Show(bool)
        self.dp_frame.Show(bool)


class Data_picker_notebook(wx.Notebook):
    def __init__(self, parent, **kwargs):
        self.logger, self.logger_name = get_logger(kwargs,
                                                   'View_data.dp_notebook')
        wx.Notebook.__init__(self, parent, **kwargs)
        self.parent = parent

        self.browse_panel = Browse_panel(self, wx.ID_ANY)
        self.AddPage(self.browse_panel, "Browse Database")
        self.logger.debug('Added browse panel')

        self.search_panel = Search_panel(self, wx.ID_ANY)
        self.AddPage(self.search_panel, "Search Database")
        self.logger.debug('Added search panel')

        self.cart_panel = Cart_panel(self, wx.ID_ANY)
        self.AddPage(self.cart_panel, "Shopping Cart")
        self.logger.debug('Added cart panel')

        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGING, self.OnPageChanging)
        self.tablist = [self.browse_panel, self.search_panel, self.cart_panel]

    def OnPageChanged(self, event):
        #old = event.GetOldSelection()
        #new = event.GetSelection()
        #sel = self.GetSelection()
        event.Skip()

    def OnPageChanging(self, event):
        old = event.GetOldSelection()
        new = event.GetSelection()
        #sel = self.GetSelection()
        pub.sendMessage("CHANGING TAB", (self.tablist[old], self.tablist[new]))
        self.logger.debug("Changing page from %s to %s" % (self.tablist[old], 
                                                           self.tablist[new]))
        event.Skip()


class BigBrotherFrame(wx.Frame):
    """
    This frame makes sure to close the other frames before itself.
    """
    def __init__( self, parent, id, **kwargs):

        wx.Frame.__init__(self, parent, id, **kwargs)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

    def OnCloseWindow(self, event):
        wx.Exit()
